package com.sitc.street_lab_2013;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.widget.Toast;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapViewActivity extends FragmentActivity {
	
	/** Constant LatLng value for Valletta, Malta. */
	private final static LatLng VALLETTA = new LatLng(35.8978,14.5141);
	
	/** Google Map object to be utilised in this activity. */
	private GoogleMap gMap;
	
	/** Called when the activity is first created. */
	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    setContentView(R.layout.map_view_activity);
	    
	    getActionBar().setTitle("Map View");
	    
	    try {
			MapsInitializer.initialize(this.getApplicationContext());
		} catch (GooglePlayServicesNotAvailableException e) {
			Toast.makeText(getApplicationContext(), "Your phone doesn't support Google Play Services. This feature is not available.", Toast.LENGTH_LONG).show();
		}
	    
	    Bundle extras = getIntent().getExtras();
	    
	    initializeMap();
	    
	    if(extras == null) {
	    	drawAllLocations();
	    }
	    else {
	    	drawLocation((SitcLoc)extras.get("location"));
	    }
	}

	/**
	 * Initialize the GoogleMap object
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void initializeMap() {
		
		FragmentManager fmanager = getSupportFragmentManager();
        Fragment fragment = fmanager.findFragmentById(R.id.map);
        SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
		
        if(gMap == null) {
			gMap = supportmapfragment.getMap();
		}
		if (gMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
		else {
			setupMap();
		}
	}
	
	private void setupMap() {
		
		/** Animate Map to Valletta */
		CameraPosition cameraPos = new CameraPosition.Builder()
		.target(VALLETTA)
		.zoom(16)
		.bearing(45)
		.build();
		gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPos));
		
		/** Map Properties */
		gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		gMap.getUiSettings().setRotateGesturesEnabled(true);
		gMap.getUiSettings().setMyLocationButtonEnabled(true);
		gMap.getUiSettings().setCompassEnabled(true);
		gMap.getUiSettings().setZoomGesturesEnabled(true);
	}
	
	private void drawAllLocations() {
		
		ArrayList<SitcLoc> locations = LocConstants.populate();
		
		for(SitcLoc loc : locations) {
			
			MarkerOptions marker = new MarkerOptions().position(loc.getLocLatLng()).title(loc.getLocDesc());
			marker.icon(BitmapDescriptorFactory.defaultMarker(loc.getLocMarker()));
			
			gMap.addMarker(marker);
		}
	}
	

	private void drawLocation(SitcLoc sitcLoc) {
		
		MarkerOptions marker = new MarkerOptions().position(sitcLoc.getLocLatLng()).title(sitcLoc.getLocDesc());
		marker.icon(BitmapDescriptorFactory.defaultMarker(sitcLoc.getLocMarker()));
		
		gMap.addMarker(marker);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initializeMap();
	}
}